<template>
  <div>
    <tableComponents
      :pagination="{ currentPage, pageSize, total: list.all_num }"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="getSearch"
      :searchData="userSearchData"
      :isHaveExport="false"
      :isHaveAdd="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          v-loading="loading"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.9%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column label="标题" min-width="150">
            <template slot-scope="scope" class="inform_title">{{ scope.row.title }}</template>
          </el-table-column>
          <el-table-column label="内容" min-width="315">
            <template slot-scope="scope">{{ scope.row.content }}</template>
          </el-table-column>
          <el-table-column label="是否读取" min-width="85">
            <template slot-scope="scope">{{ scope.row.is_read == 1 ? "已读" : "未读" }}</template>
          </el-table-column>
          <el-table-column prop="address" label="发布时间" min-width="170" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.created_time | getTimeFormat }}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button :loading="$store.state.isLoading" type="primary" size="mini" @click="examine(scope.row)">查看</el-button>
              <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="deleteNew(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" :title="forTitle" :modal-append-to-body="false" :visible.sync="centerDialogVisible" class="tl size_13" width="500px">
      <span>{{ forContent }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="centerDialogVisible = false"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { noticeSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      userSearchData: noticeSearchData,
      loading: false,
      currentPage: 1,
      list: [],
      pageSize: 10,
      multipleSelection: [],
      searchAll: {},
      centerDialogVisible: false,
      forTitle: "",
      forContent: "",
    };
  },

  components: {},
  methods: {
    deleteNew(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          console.log(1111);
          var data = { id_str: id };
          this.$api.getNewss.del_all(data).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", "出错啦！", "error");
            }
          });
        }
      });
    },
    deleteLises() {
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思",
        }).then((willDelete) => {
          if (willDelete.value) {
            var data = { id_str: str };
            this.$api.getNewss.del_all(data).then((res) => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                this.getNewsAll(this.searchAll);
              } else {
                this.$swal("删除失败！", "出错啦！", "error");
              }
            });
          }
        });
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    examine(item) {
      this.centerDialogVisible = true;
      this.forTitle = item.title;
      this.forContent = item.content;
      if (item.is_read == 0) {
        this.$api.getNewss.read({ id: item.id }).then((res) => {
          this.getNewsAll(this.searchAll);
        });
      }
    },
    getSearch(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;

      this.$api.getNewss.lists(params).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
  },
  created() {
    this.getNewsAll();
  },
  computed: {},
  watch: {},
};
</script>
<style lang="less" scoped></style>
